package com.sinai.mshab.client.widgets;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.sinai.mshab.client.Mshab;

/**
 * A simple panel which appears anywhere to notify that a field is required.
 * 
 * @author serge
 */
public class NotifyPanel extends FlexTable {

	private Label message_label;
	private Image icon;

	/**
	 * Creates a default notification
	 */
	public NotifyPanel() {
		setUp("This field is required");
	}

	/**
	 * Creates a notification with a custom message
	 * 
	 * @param message
	 *            custom error message to show if validation fails
	 */
	public NotifyPanel(String message) {
		setUp(message);
	}

	/**
	 * Set the text of the notifier.
	 * 
	 * @param text
	 *            the text to display
	 */
	public void setText(String text) {
		message_label.setText(text);
	}

	/**
	 * This puts the notify panel together.
	 * 
	 * @param error_message
	 */
	private void setUp(String error_message) {
		setStyleName("mshab-validationNotify");
		message_label = new Label(error_message);
		setWidget(0, 1, message_label);
		getCellFormatter().setVerticalAlignment(0, 0,
				HasVerticalAlignment.ALIGN_MIDDLE);
		getCellFormatter().setVerticalAlignment(0, 1,
				HasVerticalAlignment.ALIGN_MIDDLE);
	}

	/**
	 * Set a specific icon on the (0,0) position.
	 * 
	 * @param path
	 *            the path to the image of the icon.
	 */
	public void setIcon(Image icon) {
		clearCell(0, 0);
		setWidget(0, 0, icon);
	}

	/**
	 * Hides the default icon.
	 */
	public void hideIcon() {
		icon.setVisible(false);
	}

	/**
	 * Hides the background and border of the notification
	 */
	public void hideBackground() {
		String cssName = this.getStyleName();
		if (cssName.indexOf("-background") != -1) {
			setStyleName(cssName.substring(0, cssName
					.lastIndexOf("-background")));
		}
	}

	/**
	 * Shows the background and border of the notification
	 */
	public void showBackground() {
		String cssName = this.getStyleName();
		if (cssName.indexOf("-background") == -1) {
			setStyleName(cssName + "-background");
		}
	}

	/**
	 * The notification assumes the state of an info box.
	 */
	public void infoMode() {
		setIcon(Mshab.getImageBundle().getInfo().createImage());
		setStyleName("mshab-notify-info");
	}

	/**
	 * The notification assumes the state of an warning box.
	 */
	public void warningMode() {
		setIcon(Mshab.getImageBundle().getWarning().createImage());
		setStyleName("mshab-notify-warning");
	}

	/**
	 * The notification assumes the state of an error box.
	 */
	public void errorMode() {
		setIcon(Mshab.getImageBundle().getError().createImage());
		setStyleName("mshab-notify-error");
	}
}
